package com.blue.common.threadlocal;

import java.util.HashMap;
import java.util.Map;


/**
 * 分页参数设置
 * 
 * <p>标题: PageParamsThreadLocal</p>
 * <p>描述: </p>
 * <p>公司: www.yhaoquan.com</p> 
 * @作者： 杨浩泉
 * @日期：2016年11月22日
 * @版本：1.0
 */
public class PageParamsThreadLocal {

	/**
	 * 页码
	 */
	private static ThreadLocal<Integer> pageNum = new ThreadLocal<Integer>();
	
	/**
	 * 每页显示几条
	 */
	private static ThreadLocal<Integer> pageSize = new ThreadLocal<Integer>();
	
	/**
	 * 页码
	 */
	private static ThreadLocal<Integer> page = new ThreadLocal<Integer>();

	/**
	 * 每页显示几条
	 */
	private static ThreadLocal<Integer> rows = new ThreadLocal<Integer>();
	
	/**
	 * SQL 排序
	 */
	private static ThreadLocal<String> orderBy = new ThreadLocal<String>();
	
	/**
	 * SQL 排序字段
	 */
	private static ThreadLocal<String> sort = new ThreadLocal<String>();

	/**
	 * SQL 排序类型
	 */
	private static ThreadLocal<String> order = new ThreadLocal<String>();
	
	/**
	 * 分页参数集合
	 * 固定参数：
	 * 		pageNum
	 * 		pageSize
	 * 		orderBy
	 * 		page
	 * 		rows
	 * 		sort
	 * 		order
	 */
	private static ThreadLocal<Map<String, Object>> pageParams = new ThreadLocal<Map<String, Object>>();

	public static Map<String, Object> getPageParams() {
		Map<String, Object> params = new HashMap<String, Object>() ;
		params.put("pageNum", getPageNum());
		params.put("pageSize", getPageSize());
		params.put("orderBy", getOrderBy());
		params.put("page", getPage());
		params.put("rows", getRows());
		params.put("sort", getSort());
		params.put("order", getOrder());
		pageParams.set(params);
		return pageParams.get();
	}
	public static void removePageParams() {
		pageParams.remove();
	}
	
	public static Integer getPageNum() {
		if(null == pageNum.get()) setPageNum(1); ;
		return pageNum.get();
	}
	public static void setPageNum(Integer _pageNum) {
		pageNum.set(_pageNum);
	}
	public static void removePageNum() {
		pageNum.remove();
	}
	
	public static Integer getPageSize() {
		if(null == pageSize.get()) setPageSize(10);
		return pageSize.get();
	}
	public static void setPageSize(Integer _pageSize) {
		pageSize.set(_pageSize); 
	}
	public static void removePageSize() {
		pageSize.remove();
	}

	public static String getOrderBy() {
		if(null == orderBy.get()) setOrderBy("id desc");
		return orderBy.get();
	}
	public static void setOrderBy(String _orderBy) {
		orderBy.set(_orderBy);
	}
	public static void removeOrderBy() {
		orderBy.remove();
	}
	
	public static Integer getPage() {
		if(null == page.get()) setPage(1); ;
		return page.get();
	}
	public static void setPage(Integer _page) {
		page.set(_page);
	}
	public static void removePage() {
		page.remove();
	}
	
	public static Integer getRows() {
		if(null == rows.get()) setRows(10); ;
		return rows.get();
	}
	public static void setRows(Integer _rows) {
		rows.set(_rows);
	}
	public static void removeRows() {
		rows.remove();
	}
	
	public static String getSort() {
		if(null == sort.get()) setSort("id");
		return sort.get();
	}
	public static void setSort(String _sort) {
		sort.set(_sort);
	}
	public static void removeSort() {
		sort.remove();
	}
	
	public static String getOrder() {
		if(null == order.get()) setOrder("asc");
		return order.get();
	}
	public static void setOrder(String _order) {
		order.set(_order);
	}
	public static void removeOrder() {
		order.remove();
	}
	
	public static void removeAllPageParam() {
		removePageNum();
		removePageSize();
		removePage();
		removeRows();
		removeOrderBy();
		removePageParams();
		removeSort();
		removeOrder();
	}

}
